package com.parleys.activity;

import roboguice.inject.InjectView;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

import com.parleys.R;

/**
 * First screen of the application. Displays the Parleys logo. Clicking on the logo brings you to the mode selection.
 * 
 * @author David Van Droogenbroeck
 * 
 */
public class SplashActivity extends AbstractParleysActivity {

	@InjectView(R.id.image)
	private ImageView imageView;

	private final Runnable makeScreenFinish = new Runnable() {
		@Override
		public void run() {
			imageView.setVisibility(View.GONE);
			finish();
		}
	};

	public SplashActivity() {
		setCustomTitleEnabled(false);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.splash_screen);

		final Handler handler = new Handler();

		Animation tempAnimation = null;
		try {
			tempAnimation = AnimationUtils.loadAnimation(SplashActivity.this, android.R.anim.fade_out);
		} catch (NotFoundException nfe) {
			// animation is not present on the system, we do not use an animation
		}
		final Animation animation = tempAnimation;

		// make splash disappear when clicking
		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (animation != null) {
					imageView.startAnimation(animation);
					handler.postDelayed(makeScreenFinish, animation.getDuration());
				} else {
					handler.post(makeScreenFinish);
				}
			}
		});

		// make splash disappear after 2 seconds
		handler.postDelayed(new Runnable() {
			@Override
			public void run() {
				if (animation != null) {
					imageView.startAnimation(animation);
					handler.postDelayed(makeScreenFinish, animation.getDuration());
				} else {
					handler.post(makeScreenFinish);
				}
			}
		}, 2000);

	}

}
